package sign_in.loginsystem.service.realize;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import sign_in.loginsystem.mapper.DepartmentMapper;
import sign_in.loginsystem.mapper.UserMapper;
import sign_in.loginsystem.model.entity.Department;
import sign_in.loginsystem.model.entity.Search;
import sign_in.loginsystem.model.entity.User;
import sign_in.loginsystem.model.util.PageBean;
import sign_in.loginsystem.model.util.RBody;
import sign_in.loginsystem.service.port.DepartmentImplement;

import java.util.ArrayList;


@Service
public class DepartmentService implements DepartmentImplement {
    @Resource
    DepartmentMapper departmentMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public PageBean allDepartment(RBody body) {
        PageBean pageBean = body.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Page<Department> page = (Page<Department>) departmentMapper.allDepartment();
        return setDepartmentPage(pageBean,page);
    }

    @Override
    public PageBean createDepartment(RBody body) {
        departmentMapper.creatDepartment(body.getDepartment());
        return allDepartment(body);
    }

    @Override
    public PageBean editDepartment(RBody body) {
        Department department = body.getDepartment();
        Search search = body.getSearch();
        search.setUserName(body.getDepartment().getPrincipal());
        ArrayList<User> users = userMapper.adminSearch(search);
        if (users==null){
            return null;
        }
        for (User user : users) {
            if (user.getUserName().equals(body.getDepartment().getPrincipal())&&user.getDepartment()!=department.getId()) {
                Department department1 = departmentMapper.searchDepartmentById(user.getDepartment());
                department1.setNumberOfUser(department1.getNumberOfUser()-1);
                departmentMapper.editDepartment(department1);
                departmentMapper.editDepartment(department);
                departmentMapper.editDepartmentUser(user);
                return allDepartment(body);
            }
        }
        departmentMapper.editDepartment(department);
        return allDepartment(body);
    }

    @Override
    public PageBean delDepartment(RBody body) {
        System.out.println();
        System.out.println(body.getDepartment().toString());
        System.out.println();
        departmentMapper.deleteDepartment(body.getDepartment());
        return allDepartment(body);
    }

    @Override
    public PageBean registerAnOptionalDept() {
        PageBean pageBean = new PageBean();
        pageBean.setDepartments(departmentMapper.allDepartment());
        return pageBean;
    }

    public  PageBean searchUserByDepartmentId(RBody body){
        PageBean pageBean = body.getPageBean();
        PageHelper.startPage(pageBean.getPageNum(),pageBean.getPageSize());
        Page<User> page = (Page<User>) departmentMapper.searchUserByDepartmentId(body.getDepartment());
        return setUserPage(pageBean,page);
    }

    private PageBean setDepartmentPage(PageBean pageBean, Page<Department> departments) {
        pageBean.setAllObjectNum((int) departments.getTotal());
        pageBean.setDepartments((ArrayList<Department>) departments.getResult());
        return getDeptPageBean(pageBean);
    }
    private PageBean setUserPage(PageBean pageBean, Page<User> user) {
        pageBean.setAllObjectNum((int) user.getTotal());
        pageBean.setPageUser((ArrayList<User>) user.getResult());
        return getPageBean(pageBean);
    }

    private PageBean getPageBean(PageBean pageBean) {
        pageBean.setAllObjectNum(userMapper.userNum());
        if(userMapper.userNum()%pageBean.getPageSize()!=0){
            pageBean.setAllPageNum(userMapper.userNum()/pageBean.getPageSize()+1);
        }
        else {
            pageBean.setAllPageNum(userMapper.userNum()/pageBean.getPageSize());
        }
        return pageBean;
    }
    private PageBean getDeptPageBean(PageBean pageBean) {
        pageBean.setAllObjectNum(departmentMapper.allDepartmentNum());
        if(departmentMapper.allDepartmentNum()%pageBean.getPageSize()!=0){
            pageBean.setAllPageNum(departmentMapper.allDepartmentNum()/pageBean.getPageSize()+1);
        }
        else {
            pageBean.setAllPageNum(departmentMapper.allDepartmentNum()/pageBean.getPageSize());
        }
        return pageBean;
    }
}
